package com.zzyl.controller.customer;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.service.customer.CustomerReservationService;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description CustomerReservationController
 * @Author chenchuang
 * @Date 2024-08-29
 */
@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "参观预约")
public class CustomerReservationController extends BaseController{

    @Autowired
    private CustomerReservationService customerReservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Long> cancelledCount() {
        Long count = customerReservationService.cancelledCount();
        return success(count);
    }

    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult add(@RequestBody ReservationVo reservationVo) {
        customerReservationService.add(reservationVo);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public ResponseResult<PageResponse<ReservationVo>> page(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status) {
        PageResponse<ReservationVo> pageResponse = customerReservationService.getByPage(pageNum,pageSize,status);
        return success(pageResponse);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public ResponseResult cancel(@PathVariable Long id) {
        customerReservationService.cancel(id);
        return success();
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult countEachTime(@RequestParam(required = false) Long time) {
        Long endtime = time + 86400000;
        List<TimeCountVo> list = customerReservationService.countEachTime(LocalDateTimeUtil.of(time),
                LocalDateTimeUtil.of(endtime));
        return success(list);
    }
}
